//
//  SLMonthView.m
//  StarLight
//
//  Created by Duke.wu on 16/6/8.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLMonthView.h"



@interface SLMonthView ()
/**1个月*/
@property (nonatomic, strong)SLVIPMonthButton *month1Btn;
/**2个月*/
@property (nonatomic, strong)SLVIPMonthButton *month2Btn;
/**3个月*/
@property (nonatomic, strong)SLVIPMonthButton *month3Btn;
/**4个月*/
@property (nonatomic, strong)SLVIPMonthButton *month4Btn;
/**5个月*/
@property (nonatomic, strong)SLVIPMonthButton *month5Btn;

@property (nonatomic, strong)NSMutableArray *priceArr;

@end

@implementation SLMonthView


- (instancetype)init{
  if (self = [super init]) {
    self.backgroundColor = [UIColor defeutBackgroudColor];
    [self addSubview:self.month1Btn];
    [self addSubview:self.month2Btn];
    [self addSubview:self.month3Btn];
    [self addSubview:self.month4Btn];
    [self addSubview:self.month5Btn];
    [self addConstraints];
    
  }
  return self;
}

static CGFloat padding = 15;

static CGFloat BtnW = 78;

- (void)addConstraints{
  [self.month1Btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.equalTo(self).offset(padding*MIX_W);
    make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-padding*MIX_W*4)/3, BtnW*MIX_W));
  }];
  
  [self.month2Btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.centerY.equalTo(self.month1Btn);
    make.left.equalTo(self.month1Btn.mas_right).offset(padding*MIX_W);
  }];
  
  [self.month3Btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.centerY.equalTo(self.month1Btn);
    make.left.equalTo(self.month2Btn.mas_right).offset(padding*MIX_W);
  }];
  
  [self.month4Btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self).offset(padding*MIX_W);
    make.top.equalTo(self.month1Btn.mas_bottom).offset(padding*MIX_W);
    make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-padding*MIX_W*3)/2, BtnW*MIX_W));
  }];
  
  [self.month5Btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.centerY.equalTo(self.month4Btn);
    make.left.equalTo(self.month4Btn.mas_right).offset(padding*MIX_W);
  }];
  
}


- (void)btnAction:(SLVIPMonthButton *)btn{
  if(btn != self.monthBtn){
    self.monthBtn.selected = NO;
    self.monthBtn = btn;
  }
  self.monthBtn.selected=YES;
  
  self.block(self.priceArr[self.monthBtn.tag-100]);
}


- (void)returnMonthAndPrice:(ReturnMonthPrice)block{
  self.block = block;
}


- (void)configMonthView:(NSMutableArray *)priceArr{
  self.priceArr = priceArr;
  for (int i = 0; i < priceArr.count ; i ++) {
    SLVIPMonthButton *btn = [self viewWithTag:(100 + i)];
    [btn configWithModel:priceArr[i]];
  }
}



#pragma mark - getter;

- (SLVIPMonthButton *)month1Btn{
  if (!_month1Btn) {
    _month1Btn = [SLVIPMonthButton new];
    _month1Btn.tag = 100;
    [_month1Btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _month1Btn;
}

- (SLVIPMonthButton *)month2Btn{
  if (!_month2Btn) {
    _month2Btn = [SLVIPMonthButton new];
    _month2Btn.tag = 101;
    [_month2Btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _month2Btn;
}

- (SLVIPMonthButton *)month3Btn{
  if (!_month3Btn) {
    _month3Btn = [SLVIPMonthButton new];
    _month3Btn.tag = 102;
    [_month3Btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _month3Btn;
}

- (SLVIPMonthButton *)month4Btn{
  if (!_month4Btn) {
    _month4Btn = [SLVIPMonthButton new];
    _month4Btn.tag = 103;
    [_month4Btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _month4Btn;
}

- (SLVIPMonthButton *)month5Btn{
  if (!_month5Btn) {
    _month5Btn = [SLVIPMonthButton new];
    _month5Btn.tag = 104;
    [_month5Btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _month5Btn;
}


@end
